/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webOffline;

import java.util.ArrayList;

/**
 *
 * @author Do Viet Trung
 */
public class ProjectAddress {
    private String projectName;
    private String address;
    private int upLink;
    private String saveTo;
    private ArrayList <Pattern> arrayPattern;
    ArrayList <String> listExt;
    static final private String PROJECT_FOLDER = WebOffline.projectsPath;
    
    public ProjectAddress() throws Exception {
        arrayPattern = new ArrayList<Pattern>();
        listExt = new ArrayList<String>();
        listExt = DOMXml.readFileTypes(Option.FileTypePath);
    }
    
    public ProjectAddress(String projName,
                          String address,
                          int upLink,
                          String saveToDir) throws Exception {
        this.projectName = projName;
        this.address = address;
        this.upLink = upLink;

        if (saveToDir.equals("") || saveToDir == null) {
            // set saveTo directory by default
            this.saveTo = PROJECT_FOLDER + this.projectName + "/";
        }
        else {
            this.saveTo = saveToDir;
        }

        arrayPattern = new ArrayList<Pattern>();
        listExt = new ArrayList<String>();
        listExt = DOMXml.readFileTypes(Option.FileTypePath);
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public ArrayList<Pattern> getArrayPattern() {
        return arrayPattern;
    }

    public void removePatternIndex(int index) {
        if(index >= 0 && index < arrayPattern.size()) {
            arrayPattern.remove(index);
        }
    }

    public void setArrayPattern(ArrayList<Pattern> arrayPattern)throws Exception {
        this.arrayPattern = arrayPattern;
//        for(int i = 0; i < this.arrayPattern.size(); i++)
//        {
//            this.arrayPattern.get(i).createExtension(listExt);
//        }

    }

    public void setArrayPatternIndex (ArrayList<Pattern> arrayPattern,
                                      int index)
                                      throws Exception {
        this.arrayPattern = arrayPattern;
        this.arrayPattern.get(index).createExtension(listExt);
    }

    public ArrayList<String> getListExt() {
        return listExt;
    }

    public void setListExt(ArrayList<String> listExt) {
        this.listExt = listExt;
    }
    
    public int getIndexOfPattern(String pattern) {
        for(int i = 0; i < arrayPattern.size(); i++) {
            if(arrayPattern.get(i).getPattern().equals(pattern)) {
                return i;
            }
        }

        return -1;
    }

    public PermissionPattern getPermissionPatternOf(String patternString) {
        for(int i = 0; i < this.arrayPattern.size();i++) {
            Pattern pattern = this.arrayPattern.get(i);
            
            if(pattern.getPattern().equals(patternString)) {
                return pattern.getPermission();
            }
        }

        return null;
    }
    
    public ArrayList<String> getListExtensionsOf(String pattern) {
        for(int i = 0; i < arrayPattern.size(); i++) {
            Pattern tmp = arrayPattern.get(i);
            
            if(tmp.getPattern().equals(pattern)) {
                return tmp.getListExtensions();
            }
        }

        return null;
    }
    
    public ArrayList<Extension> getArrayExtensionsOf(String pattern) {
        for(int i = 0; i < arrayPattern.size(); i++) {
            Pattern tmp = arrayPattern.get(i);
            
            if(tmp.getPattern().equals(pattern)) {
                return tmp.getArrayExtension();
            }
        }

        return null;
    }
     
    public ArrayList<Pattern> filterPattern(PermissionPattern permisson) {
        ArrayList<Pattern> res = new ArrayList<Pattern>();

        for(int i = 0; i < this.arrayPattern.size(); i++) {
            Pattern pattern = this.arrayPattern.get(i);

            if(pattern.getPermission() == permisson) {
                res.add(pattern);
            }
        }

        return res;
    }
    
    public ArrayList<Pattern> filterPatterns(PermissionPattern permisson1,
                                             PermissionPattern permission2)
    {
        ArrayList<Pattern> res = new ArrayList<Pattern>();
        
        for(int i = 0; i < this.arrayPattern.size(); i++) {
            Pattern pattern = this.arrayPattern.get(i);

            if(pattern.getPermission() == permisson1 
                    || pattern.getPermission() == permission2) {
                res.add(pattern);
            }
        }

        return res;
    }
    
    public ArrayList<Pattern> filterLimitedPatterns() {
        ArrayList<Pattern> res = new ArrayList<Pattern>();
        
        for(int i = 0; i < this.arrayPattern.size(); i++) {
            Pattern pattern = this.arrayPattern.get(i);
            
            if(PatternType.indentifyTpe(pattern.getPattern()) == PatternType.LIMIT) {
                res.add(pattern);
            }
        }

        return res;
    }

    public void updateArrayExtension(ArrayList<Extension> arrayExtension, String pattern) {
        int index = getIndexOfPattern(pattern);

        if (index < 0 && pattern.endsWith(Pattern.LIMIT_PATTERN + Pattern.LIMIT_PATTERN)) {
            String tmp = pattern.substring(0, pattern.length() -
                    Pattern.LIMIT_PATTERN.length() - Pattern.LIMIT_PATTERN.length());
            index = getIndexOfPattern(tmp + Pattern.LIMIT_PATTERN);
            this.arrayPattern.get(index).setPattern(tmp);
            pattern = tmp;
        }
        
        if (index < 0 && pattern.endsWith(Pattern.LIMIT_PATTERN)) {
            String tmp = pattern.substring(0, pattern.length() - Pattern.LIMIT_PATTERN.length());
            index = getIndexOfPattern(tmp);
        }
        
        if (index >= 0) {
            if(PatternType.indentifyTpe(this.arrayPattern.get(index).getPattern()) != PatternType.LIMIT)
            {
                if(this.arrayPattern.get(index).getPermission() == PermissionPattern.ALLOW_ALL)
                {
                    this.arrayPattern.get(index).setPermission(PermissionPattern.ALLOW_ITEMS);
                }
                else if(this.arrayPattern.get(index).getPermission() == PermissionPattern.DENY_ALL)
                {
                    this.arrayPattern.get(index).setPermission(PermissionPattern.DENY_ITEMS);
                }
                else if(this.arrayPattern.get(index).getPermission() == PermissionPattern.DENY_ITEMS)
                {
                    this.arrayPattern.get(index).setPermission(PermissionPattern.DENY_ALL);
                }
                else if(this.arrayPattern.get(index).getPermission() == PermissionPattern.ALLOW_ITEMS)
                {
                    this.arrayPattern.get(index).setPermission(PermissionPattern.ALLOW_ALL);
                }
            }

            if (pattern.endsWith(Pattern.LIMIT_PATTERN))
            {
                this.arrayPattern.get(index).setPattern(pattern);
            }

            this.arrayPattern.get(index).setArrayExtension(arrayExtension);
        }
    }
    
    public void updateArrayPattern(ArrayList<Pattern> arrayPattern) {
        if(this.arrayPattern.size() <= arrayPattern.size()) {//origin <= change
            int i = 0;
            for(; i < this.arrayPattern.size(); i++) {
                Pattern origin = this.arrayPattern.get(i);
                Pattern change = arrayPattern.get(i);
                if(PatternType.indentifyTpe(origin.getPattern()) == PatternType.LIMIT)
                {
                    if((change.getPermission() == PermissionPattern.ALLOW_ALL
                            && origin.getPermission() == PermissionPattern.ALLOW_ITEMS
                            && origin.getPattern().equals(change.getPattern() + Pattern.LIMIT_PATTERN))
                            || (change.getPermission() == PermissionPattern.DENY_ALL
                            && origin.getPermission() == PermissionPattern.DENY_ITEMS
                            && origin.getPattern().equals(change.getPattern() + Pattern.LIMIT_PATTERN)))
                    {
                        continue;
                    }
                    else {
                        this.arrayPattern.set(i,change);
                    }
                }
                else if(!origin.getPattern().equals(change.getPattern())
                        || origin.getPermission() != change.getPermission())
                {
                    this.arrayPattern.set(i,change);
                }
            }

            for(;i< arrayPattern.size();i++) {
                this.arrayPattern.add(arrayPattern.get(i));
            }

        }
        else {//origin > change
            int i = 0;
            for(i = 0; i < arrayPattern.size(); i++) {
                Pattern origin = this.arrayPattern.get(i);
                Pattern change = arrayPattern.get(i);

                if(PatternType.indentifyTpe(origin.getPattern()) == PatternType.LIMIT) {
                    if((change.getPermission() == PermissionPattern.ALLOW_ALL
                            && origin.getPermission() == PermissionPattern.ALLOW_ITEMS
                            && origin.getPattern().equals(change.getPattern() + Pattern.LIMIT_PATTERN))
                            || (change.getPermission() == PermissionPattern.DENY_ALL
                            && origin.getPermission() == PermissionPattern.DENY_ITEMS
                            && origin.getPattern().equals(change.getPattern() + Pattern.LIMIT_PATTERN)))
                    {
                        continue;
                    }
                    else {
                        this.arrayPattern.set(i,change);
                    }
                }
                else if(!origin.getPattern().equals(change.getPattern())
                        || origin.getPermission() != change.getPermission())
                {
                    this.arrayPattern.set(i, change);
                }
            }

            for(;i < this.arrayPattern.size(); i++) {
                this.arrayPattern.remove(i);
                i--;
            }
        }
    }

    public String getSaveTo() {
        return saveTo;
    }

    public void setSaveTo(String saveToDir) {
        if (saveToDir.equals("") || saveToDir == null) {
            // set saveTo directory by default
            this.saveTo = PROJECT_FOLDER;
        }
        else {
            this.saveTo = saveToDir;
        }
    }

    public int getUpLink() {
        return upLink;
    }

    public void setUpLink(int upLink) {
        this.upLink = upLink;
    }
}